package splug.cjk;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

import splug.cjk.packet.LoginPacket;

import android.app.Activity;
import android.content.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class LetsGoActivity extends Activity {

	private EditText ed_id;
	private EditText ed_pw;
	public static String server_ip = "220.70.0.16";

	//304호 서버 IP : 220.70.0.16
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		//회원가입 버튼, 로그인 버튼, 찾기 버튼
		ImageButton btn_assign = (ImageButton)findViewById(R.id.btn_join);
		ImageButton btn_login = (ImageButton)findViewById(R.id.btn_login);
		ImageButton btn_find = (ImageButton)findViewById(R.id.button_find);

		//아이디, 비밀번호 텍스트 뷰
		ed_id = (EditText) findViewById(R.id.text_main_id);
		ed_pw = (EditText) findViewById(R.id.text_main_passwd);

		btn_assign.setOnClickListener(assign);
		btn_login.setOnClickListener(login);
		btn_find.setOnClickListener(find);
	}

	private View.OnClickListener assign = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			Toast.makeText(LetsGoActivity.this, "회원가입", Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(LetsGoActivity.this, Assign.class);
			startActivity(intent);
		}
	};

	private View.OnClickListener login = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
//			String id = ed_id.getText().toString();
//			Intent intent = new Intent(LetsGoActivity.this, Menu.class);
//			Bundle b = new Bundle();
//			//ID를 다음 액티비티로 넘겨준다.
//			b.putString("ID", id);
//			intent.putExtras(b);
//			startActivity(intent);

			try {
				String id = ed_id.getText().toString();
				String pw = ed_pw.getText().toString();

				if(id.equals("")){
					Toast.makeText(LetsGoActivity.this, "ID를 입력해주세요", Toast.LENGTH_SHORT).show();
				}
				else if(pw.equals("")){
					Toast.makeText(LetsGoActivity.this, "PW를 입력해주세요", Toast.LENGTH_SHORT).show();
				}
				else{
					Socket socket = new Socket(server_ip, 2007);

					InputStream is = socket.getInputStream();
					OutputStream os = socket.getOutputStream();
					
					OutputStreamWriter osw = new OutputStreamWriter(os);
					osw.write('1');
					osw.flush();
					
					InputStreamReader isr = new InputStreamReader(is);
					int ack = isr.read();
					
					if(ack == '1'){
						LoginPacket packet = new LoginPacket(id,pw);					
						ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
						output.writeObject(packet);

						ack = isr.read();

						if(ack == '1'){
							Toast.makeText(LetsGoActivity.this, id + "님 환영합니다.", Toast.LENGTH_LONG).show();
							Intent intent = new Intent(LetsGoActivity.this, Menu.class);
							Bundle b = new Bundle();
							//ID를 다음 액티비티로 넘겨준다.
							b.putString("ID", id);
							intent.putExtras(b);
							startActivity(intent);
						}
						else{
							Toast.makeText(LetsGoActivity.this, "ID와 비밀번호가 일치하지 않습니다.", Toast.LENGTH_SHORT).show();
						}
					}
					else{
						Toast.makeText(LetsGoActivity.this, "네트워크 에러", Toast.LENGTH_SHORT).show();
					}
				}

			} catch (Exception e) {
				Toast.makeText(LetsGoActivity.this, "네트워크 에러", Toast.LENGTH_SHORT).show();
			} 

		}
	};

	private View.OnClickListener find = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(LetsGoActivity.this, FindID.class);
			startActivity(intent);
		}
	};
}